define(function (require) {
    var doT = require('doT');
    require('table');
    require('treegrid');
    require('bootstrapValidator');
    require('messager');
    require('doctor-select');
    require('doctor-data');

    $.module("BASE.organizational_structure", function () {
        return {
            init: function () {
                // this.loadData();
                this.loadEvent();
            },
            loadData: function (orgVersionId) {
                SYS.Core.ajax({
                    url: 'base/ors/list',
                    common: {},
                    data: {
                        orgVersionId: orgVersionId
                    },
                    success: function (data) {
                        var template = doT.template($("#tmpl_sys_menu_list").html());
                        $("#sys_menu_list").html(template(data.data));

                        $('#menu_tree').treegrid();
                        $('#menu_tree').treegrid('collapseAll');
                    }
                })
            },
            loadEvent: function () {
                var that = this;

                // 定义编辑框form
                let $form = $("#fm");

                // 查询
                $("#search-toolbar").on('click', "#btn_search", function () {
                    let val = $('#s_orgVersionId').val();
                    let id = $('#s_orgVersionId').attr('data-id');
                    if (val == "" && id == "") {
                        $.messager.popup("请选择组织机构版本，在进行查询", "warn");
                    }

                    that.loadData(id);
                }).on("keydown ", "input", function (e) {
                    if (e.keyCode == 13) {
                        let val = $('#s_orgVersionId').val();
                        let id = $('#s_orgVersionId').attr('data-id');
                        if (val == "" && id == "") {
                            $.messager.popup("请选择组织机构版本，在进行查询", "warn");
                        }

                        that.loadData(id);
                    }
                });

                // 报表项目父级
                $("#s_orgVersionId").on('click', function () {
                    DOCTOR.Select.showOrgVersion(this);
                });

                $('#fm').bootstrapValidator({
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        // Company: {
                        //     validators: {
                        //         notEmpty: {
                        //             message: '此项必填'
                        //         }
                        //     }
                        // },
                        // sort: {
                        //     validators: {
                        //         notEmpty: {
                        //             message: '此项必填'
                        //         },
                        //         regexp: {
                        //             regexp: /^[1-9][0-9]*$/,
                        //             message: '请输入正整数'
                        //         }
                        //     }
                        // }
                    },
                    //提交后，整个页面会刷新
                    //submitHandler: function(validator, form, submitButton) {
                    //    that.save);
                    //}
                }).on('success.form.bv', function (e) { //使用此方法可以ajax
                    e.preventDefault();
                    //保存菜单
                    that.save();
                });

                //清空验证信息(必须)
                $('#myModal').on('hide.bs.modal', function() {
                    $("#fm input[name='_id']").val('');
                    $("#fm input[name='parent_id']").val('');
                    $("#fm input[name='Company']").val('');
                    $("#fm input[name='Depart']").val('');
                    $("#fm input[name='Center']").val('');
                    $("#fm input[name='NoeLevel']").val('');
                    $("#fm input[name='sort']").val(1);

                    $('#fm').bootstrapValidator('resetForm', true);
                });
            },
            editDialog: function (id) {
                //根据id获取菜单信息
                SYS.Core.ajax({
                    url: "base/ors/getById",
                    common: {},
                    data: {id: id },
                    success: function (data) {
                        if(data.code == 1){
                            //填充表单
                            let obj = data.data;

                            $("#fm input[name='_id']").val(obj.id);
                            $("#fm input[name='parent_id']").val(obj.parent_id);
                            $("#fm input[name='Company']").val(obj.Company);
                            $("#fm input[name='Depart']").val(obj.Depart);
                            $("#fm input[name='Center']").val(obj.Center);
                            $("#fm input[name='NoeLevel']").val(obj.NoeLevel);
                            $("#fm input[name='sort']").val(obj.sort);
                        }else{
                            $.messager.popup('加载数据失败，请稍后再试','error');
                        }
                    }
                });
                $('#myModal').modal({backdrop: 'static', keyboard: false});
                $('#myModal').modal('show');
            },
            save: function () {
                var that = this;

                let url = '';
                let id = $("#fm input[name='_id']").val();
                if(id==''){
                    url = 'base/ors/save';
                }else{
                    url = 'base/ors/edit';
                }
                let parent_id = $("#fm input[name='parent_id']").val();
                let Company = $("#fm input[name='Company']").val();
                let Depart = $("#fm input[name='Depart']").val();
                let Center = $("#fm input[name='Center']").val();
                let NoeLevel = $("#NoeLevel").val();
                let sort = $("#fm input[name='sort']").val();

                SYS.Core.ajax({
                    url: url,
                    common: {},
                    data: {
                        id: id ? id : 0,
                        parent_id: parent_id ? parent_id : 0,
                        Company: Company,
                        Depart: Depart,
                        Center: Center,
                        NoeLevel: NoeLevel,
                        sort: sort
                    },
                    success: function (data) {
                        if(data.code == 1){
                            $('#myModal').modal('hide');
                            $.messager.popup('保存成功','success');
                            that.loadData();
                        }else{
                            $('#fm').bootstrapValidator('disableSubmitButtons', false);
                            $.messager.popup('保存失败','error');
                        }
                    }
                })
            },
            addChildMenu: function (id, NoeLevel) {
                $("#fm input[name='parent_id']").val(id); //绑定父菜单ID

                if(NoeLevel == undefined){
                    $("#fm input[name='NoeLevel']").val(0);                     // 节点所在层级
                } else {
                    $("#fm input[name='NoeLevel']").val(Number(NoeLevel) + 1);  // 节点所在层级
                }

                $('#myModal').modal('show');
            },
            deleteMenu: function (id) {
                var that = this;
                $.messager.confirm('确定要删除选择的组织架构吗？如果存在子组织架构，将一并删除？', function () {
                    SYS.Core.ajax({
                        url: "base/ors/del",
                        common: {},
                        data: {id: id},
                        success: function (data) {
                            if(data.code == 1){
                                $.messager.popup(data.msg, 'success');
                                that.loadData();
                            }else{
                                $.messager.popup('删除失败，请稍后再试', 'error');
                            }
                        }
                    })
                });
            }
        }
    });

    BASE.organizational_structure.init();
});
